<template>
<div class="app-container calendar-list-container">
  <div class="filter-container">
        <el-date-picker v-model="listQuery.createTime" type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择开始时间"></el-date-picker>
        <el-date-picker v-model="listQuery.shutTime" type="date" :picker-options="pickerSelectBeginDateAfter" format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择结束时间"></el-date-picker>
        <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">查询</el-button>
        <el-button class="filter-item"  v-if="integralManager_btn_add"  style="margin-left: 10px;" @click="handleCreate" type="primary" icon="plus">新增</el-button>
      </div>
  <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
        <el-table-column align="center" label="编号" width="200"> <template scope="scope">
              <span>{{scope.row.integralId}}</span>
        </template> </el-table-column>
        <el-table-column width="120" align="center" label="总额"> <template scope="scope">
            <span>{{scope.row.integralAmtInc}}</span>
        </template> </el-table-column>
        <el-table-column width="100" align="center" label="面值"> <template scope="scope">
                <span>{{scope.row.integralAmt}}</span>
        </template> </el-table-column>
        <el-table-column width="150" align="center" label="触发事件"> <template scope="scope">
              <span>{{scope.row.incCode}}</span>
        </template> </el-table-column>
        <el-table-column width="190" align="center" label="领取开始时间"> <template scope="scope">
                <span>{{scope.row.createTime}}</span>
        </template> </el-table-column>
        <el-table-column width="190" align="center" label="领取结束时间"> <template scope="scope">
                <span>{{scope.row.shutTime}}</span>
              </template> </el-table-column>
        <el-table-column width="90" align="center" label="发行量"> <template scope="scope">
                <span>{{scope.row.incNum}}</span>
        </template> </el-table-column>
        <el-table-column width="110" align="center" label="是否可领取"> <template scope="scope">
          <span v-if="scope.row.integralStatus == '00'">可领取</span>
          <span v-else>不可领取</span>      
        </template> </el-table-column>
        <el-table-column width="200" align="center" label="积分描述"> <template scope="scope">
                <span>{{scope.row.integraliInfo}}</span>
        </template> </el-table-column>
        <el-table-column align="center" label="操作" width="300px"> <template scope="scope">
               <el-button v-if="integralManager_btn_info" size="small" type="success" @click="handleGetInfo(scope.row)">查看</el-button>
               <el-button v-if="scope.row.integralStatus == '01' && scope.row.isOpen == '00' && integralManager_btn_info" size="small" type="success" @click="handleUpdate(scope.row)">修改</el-button>
               <el-button v-if="scope.row.integralStatus == '00' && integralManager_btn_stop" size="small" type="danger" @click="handleStop(scope.row)">停用</el-button>
      </template> </el-table-column>
  </el-table>
  <div v-show="!listLoading" class="pagination-container">
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
  </div>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
    <el-form :model="form" :rules="rules" ref="form" label-width="150px">
      <el-form-item label="面值" prop="integralAmt">
        <el-input type="number" v-model="form.integralAmt" placeholder="请输入面值"></el-input>
      </el-form-item>
      <el-form-item label="发行量" prop="incNum">
        <el-input type="number" v-model="form.incNum" placeholder="请输入发行量"></el-input>
      </el-form-item>
      <el-form-item label="可触发的事件编号" prop="incCode">
        <el-input v-model="form.incCode" placeholder="请选择事件" v-on:click.native="handlerChooseIncident"></el-input>
      </el-form-item>
      <el-form-item label="领取开始时间" prop="createTime">
        <el-date-picker v-model="form.createTime" type="date" :picker-options="pickerRevDateBegin" format="yyyy-MM-dd" placeholder="请选择开始时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="领取结束时间" prop="shutTime">
        <el-date-picker v-model="form.shutTime" type="date" :picker-options="pickerRevDateAfter" format="yyyy-MM-dd" placeholder="请选择结束时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="每天限领次数" prop="everyDay">
         <el-input type="number" v-model="form.everyDay" placeholder="请输入每天限领次数"></el-input>
      </el-form-item>
      <el-form-item label="每周限领次数" prop="everyWeek">
         <el-input type="number" v-model="form.everyWeek" placeholder="请输入每周限领次数"></el-input>
      </el-form-item>
      <el-form-item label="每月限领次数" prop="everyMonth">
         <el-input type="number" v-model="form.everyMonth" placeholder="请输入每月限领次数"></el-input>
      </el-form-item>
       <el-form-item label="积分状态">
              <el-select class="filter-item" v-model="form.integralStatus" placeholder="请选择">
                <el-option label="可领" value="00"> </el-option>
                <el-option label="不可领" value="01"> </el-option>
              </el-select>
            </el-form-item>
      <el-form-item label="简介">
        <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 5}" placeholder="请输入内容" v-model="form.incRemark"> </el-input>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel('form')">取 消</el-button>
      <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
      <el-button v-else type="primary" @click="update('form')">确 定</el-button>
    </div>
  </el-dialog>

<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible2">
    <el-form :model="form"  ref="form" label-width="150px">
      <el-form-item label="面值" prop="integralAmt">
        <el-input type="number" v-model="form.integralAmt" placeholder="请输入面值"></el-input>
      </el-form-item>
      <el-form-item label="发行量" prop="incNum">
        <el-input type="number" v-model="form.incNum" placeholder="请输入发行量"></el-input>
      </el-form-item>
      <el-form-item label="领取开始时间: " prop="createTime">
        <label>{{form.createTime}}</label>
      </el-form-item>
      <el-form-item label="领取结束时间: " prop="shutTime">
        <label>{{form.shutTime}}</label>
      </el-form-item>
      <el-form-item label="每天限领次数" prop="everyDay">
         <el-input type="number" v-model="form.everyDay" placeholder="请输入每天限领次数"></el-input>
      </el-form-item>
      <el-form-item label="每周限领次数" prop="everyWeek">
         <el-input type="number" v-model="form.everyWeek" placeholder="请输入每周限领次数"></el-input>
      </el-form-item>
      <el-form-item label="每月限领次数" prop="everyMonth">
         <el-input type="number" v-model="form.everyMonth" placeholder="请输入每月限领次数"></el-input>
      </el-form-item>
      <el-form-item label="可触发的事件编号" prop="incCode">
        <el-input v-model="form.incCode" placeholder="请选择事件" v-on:click.native="handlerChooseIncident"></el-input>
      </el-form-item>
       <el-form-item label="积分状态">
              <el-select class="filter-item" v-model="form.integralStatus" placeholder="请选择">
                <el-option label="可领" value="00"> </el-option>
                <el-option label="不可领" value="01"> </el-option>
              </el-select>
            </el-form-item>
      <el-form-item label="简介">
        <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 5}" placeholder="请输入内容" v-model="form.integraliInfo"> </el-input>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel('form')">取 消</el-button>
      <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
      <el-button v-else type="primary" @click="update('form')">确 定</el-button>
    </div>
  </el-dialog>

  <!-- 事件列表 -->
  <el-dialog :title="textMap[dialogStatus1]" :visible.sync="dialogIncVisible">
    <div v-for="inc in incidentList" v-bind:key="inc.incCode">
    <input type="checkbox" id="incident" :value=inc.incCode v-model="checkedIncidents" v-bind:key="inc.incName">
    <label for="incident">{{inc.incName}}</label>
    </div>
    <br>
    <!-- <span>选择的事件: {{ checkedIncidents }}</span> -->

    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelIncident('dialogIncVisible')">取 消</el-button>
      <el-button type="primary" @click="selectIncident('dialogIncVisible')">确 定</el-button>
    </div>
  </el-dialog>

  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible1">
      <el-form :model="form" ref="form" label-width="150px">
        <el-form-item label="面值: " prop="integralAmt">
          <span>{{form.integralAmt}}</span>
        </el-form-item>
        <el-form-item label="发行量: " prop="incNum">
          <span>{{form.incNum}}</span>
        </el-form-item>
        <el-form-item label="活动开始时间: " prop="createTime">
          <span>{{form.createTime}}</span>
        </el-form-item>
        <el-form-item label="活动结束时间: " prop="shutTime">
          <span>{{form.shutTime}}</span>
        </el-form-item>
        <el-form-item label="每天限领次数: " prop="everyDay">
          <span>{{form.everyDay}}次</span>
        </el-form-item>
        <el-form-item label="每周限领次数: " prop="everyWeek">
          <span>{{form.everyWeek}}次</span>
        </el-form-item>
        <el-form-item label="每月限领次数: " prop="everyMonth">
          <span>{{form.everyMonth}}次</span>
        </el-form-item>
        <el-form-item label="可触发的事件编号: " prop="incCode">
          <span>{{form.incCode}}</span>
        </el-form-item>
        <el-form-item label="积分状态">
          <span v-if="form.integralStatus == '00'">可领取</span>
          <span v-else>不可领取</span>         
        </el-form-item>
        <el-form-item label="简介: ">
          <span>{{form.integraliInfo}}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
</div>
</template>

<script>
import {
  page,
  addObj,
  getObj,
  delObj,
  putObj,
  getInfo,
  stopObj
} from '@/api/biz/operative/integral/index'
import { getInc } from '@/api/biz/operative/coupon/index'
import { mapGetters } from 'vuex'
export default {
  name: 'user',
  data() {
    return {
      form: {
        integralStatus: '01',
        integralAmt: '',
        incNum: '',
        createTime: '',
        shutTime: '',
        actId: '',
        everyDay: '',
        everyMonth: '',
        everyWeek: '',
        incCode: '',
        sceneId: '',
        incRemark: '',
        actToken: ''
      },
      pickerSelectBeginDateAfter: {
        disabledDate: (time) => {
          return time.getTime() < this.listQuery.createTime
        }
      },
      pickerRevDateBegin: {
        disabledDate: (time) => {
          return time.getTime() < (Date.now() - 24 * 60 * 60 * 1000)
        }
      },
      pickerRevDateAfter: {
        disabledDate: (time) => {
          return time.getTime() < this.form.createTime
        }
      },
      checkedIncidents: [],
      checkedScenes: [],
      rules: {
        integralAmt: [
          {
            required: true,
            message: '请输入面值 (数字)',
            trigger: 'blur'
          }
        ],
        incNum: [
          {
            required: true,
            message: '请输入发行量 (数字)',
            trigger: 'blur'
          }
        ],
        everyDay: [
          {
            required: true,
            message: '请输入每天限领次数 (数字)',
            trigger: 'blur'
          }
        ],
        everyWeek: [
          {
            required: true,
            message: '请输入每周限领次数 (数字)',
            trigger: 'blur'
          }
        ],
        everyMonth: [
          {
            required: true,
            message: '请输入每月限领次数 (数字)',
            trigger: 'blur'
          }
        ],
        integralStatus: [
          {
            required: true,
            message: '请选择领取状态',
            trigger: 'blur'
          }
        ]
        // sceneId:[
        //   {
        //     required: true,
        //     message: '请选择使用场景',
        //     trigger: 'blur'
        //   }
        // ],
        // incCode:[
        //   {
        //     required: true,
        //     message: '请选择触发事件',
        //     trigger: 'blur'
        //   }
        // ]
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        createTime: '',
        shutTime: '',
        actId: ''
      },
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      dialogSceneVisible: false,
      dialogIncVisible: false,
      dialogStatus: '',
      dialogStatus1: '',
      sceneList: null,
      incidentList: null,
      actId: '',
      integralManager_btn_info: false,
      integralManager_btn_edit: false,
      integralManager_btn_stop: false,
      integralManager_btn_add: false,
      integralManager_btn_get: false,
      textMap: {
        update: '编辑',
        create: '创建',
        info: '详情',
        selectScene: '选择场景',
        selectInc: '选择事件'
      },
      tableKey: 0
    }
  },
  created() {
    this.getList()
    this.integralManager_btn_info = this.elements['integralManager:btn_info']
    this.integralManager_btn_edit = this.elements['integralManager:btn_edit']
    this.integralManager_btn_stop = this.elements['integralManager:btn_stop']
    this.integralManager_btn_add = this.elements['integralManager:btn_add']
    this.integralManager_btn_get = this.elements['integralManager:btn_get']
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  mounted() {
    console.log('enen ' + this.$route.query.actInfo.actId)
    this.listQuery.actId = this.$route.query.actInfo.actId
    this.actId = this.$route.query.actInfo.actId
    this.form.actId = this.$route.query.actInfo.actId
    console.log(this.actId)
  },
  methods: {
    getList() {
      this.listLoading = true
      page(this.listQuery)
        .then(response => {
          this.list = response.data.rows
          console.log(this.list)
          this.total = response.data.total
          this.listLoading = false
        })
    },
    handleFilter() {
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleStop(row) {
      this.$confirm('此操作将停止活动, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          stopObj(row)
            .then(() => {
              this.getList()
              this.$notify({
                title: '成功',
                message: '停用成功',
                type: 'success',
                duration: 2000
              })
              const index = this.list.indexOf(row)
              this.list.splice(index, 1)
            })
        })
    },
    handleUpdate(row) {
      getObj(row)
        .then(response => {
          console.log(response)
          this.dialogFormVisible2 = true
          this.dialogStatus = 'update'
          this.form = response.data
        })
    },
    handleGetInfo(row) {
      getInfo(row)
        .then(response => {
          console.log(response)
          this.dialogFormVisible1 = true
          this.dialogStatus = 'info'
          this.form = response.data
        })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delObj(row)
            .then(() => {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              })
              const index = this.list.indexOf(row)
              this.list.splice(index, 1)
            })
        })
    },
    selectedScene() {
      this.dialogSceneVisible = false
      console.log('选择的场景: ' + this.checkedScenes)
      var arr = this.checkedScenes
      this.form.sceneId = arr.join()
    },
    cancelScene(formName) {
      this.dialogSceneVisible = false
    },
    selectIncident(formName) {
      this.dialogIncVisible = false
      console.log('选择的事件: ' + this.checkedIncidents)
      var arr = this.checkedIncidents
      this.form.incCode = arr.join()
    },
    cancelIncident(formName) {
      this.dialogIncVisible = false
    },
    handlerChooseScene() {
      this.dialogSceneVisible = true
      this.dialogStatus1 = 'selectScene'
    },
    handlerChooseIncident() {
      this.dialogIncVisible = true
      this.dialogStatus1 = 'selectInc'
      getInc()
        .then(response => {
          this.incidentList = response
        })
    },
    create(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          console.log(this.form)
          this.form.actId = this.actId
          this.$confirm('此操作将创建积分, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              addObj(this.form)
                .then(() => {
                  this.dialogFormVisible = false
                  this.getList()
                  this.$notify({
                    title: '成功',
                    message: '创建成功',
                    type: 'success',
                    duration: 2000
                  })
                })
            })
        } else {
          return false
        }
      })
    },
    cancel(formName) {
      this.dialogFormVisible = false
      this.dialogFormVisible2 = false
      this.$refs[formName].resetFields()
    },
    update(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible2 = false
          putObj(this.form).then(() => {
            this.dialogFormVisible2 = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
    resetTemp() {
      this.form = {
        integralStatus: '01',
        integralAmt: '',
        incNum: '',
        createTime: '',
        shutTime: '',
        everyDay: '',
        everyMonth: '',
        everyWeek: '',
        incCode: '',
        sceneId: '',
        incRemark: '',
        actToken: '',
        checkedIncidents: [],
        checkedScenes: []
      }
    }
  }
}
</script>

<style scoped>
label {
    margin: 0px 0px !important;
}
</style>
